package com.yc.controller;

import com.yc.biz.CartInfoBiz;
import com.yc.entity.CartInfo;
import com.yc.entity.MemberInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: freshgoodPro
 * @description:
 * @author: Joker
 * @create: 2023-09-16 19:49
 */
@Slf4j
@RestController
@RequestMapping("cart")
public class CartInfoController {
    @Autowired
    private CartInfoBiz cartInfoBiz;
    /**
     * 根据会员编号查看
     * @return
     */
    @RequestMapping(value = "findCart",method = RequestMethod.GET)
    public List<Map<String, Object>> findByMno(@RequestParam Integer mno){
        List<Map<String, Object>> carts = cartInfoBiz.findByMno(mno);
        return carts;
    }
    /**
     * 添加购物车
     * @return
     */
    @RequestMapping(value = "addCart",method = RequestMethod.GET)
    public String addCart(@RequestParam Integer num,@RequestParam Integer gno, HttpSession session){
        CartInfo cartInfo = new CartInfo();
        cartInfo.setGno(gno);
        cartInfo.setNum(num);
        MemberInfo memberInfo = (MemberInfo) session.getAttribute("user");
        cartInfo.setMno(memberInfo.getMno());
        //添加购物车返回当前数据的编号
        String cno = cartInfoBiz.addCart(cartInfo);
        System.out.println(session.getId());
        List<Map<String, Object>> carts = (List<Map<String, Object>>) session.getAttribute("cartList");
        if (carts == null) {
            carts = new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("cno", cno);
        map.put("gno", cartInfo.getGno());
        map.put("num", 1);
        map.put("mno", memberInfo.getMno());
        carts.add(map);
        session.setAttribute("cartList", carts);
        return cno;
    }
    /**
     * 根据购物车编号更新商品数量
     * @param cno num
     * @return
     */
    @RequestMapping(value = "updateCart",method = RequestMethod.GET)
    public int updateCart(@RequestParam String cno, @RequestParam Integer num, HttpSession session){
        CartInfo cartInfo = new CartInfo();
        cartInfo.setCno(cno);
        cartInfo.setNum(num);
        int result = cartInfoBiz.update(cartInfo);
        if (result > 0) {
            List<Map<String, Object>> carts = (List<Map<String, Object>>)session.getAttribute("cartList");
            for (int i = 0; i < carts.size(); i++) {
                Map<String, Object> m = carts.get(i);
                if(cartInfo.getCno().equals(m.get("cno").toString())){
                    int nums = Integer.parseInt(m.get("num").toString());
                    carts.get(i).put("num", nums + cartInfo.getNum());
                    break;
                }
            }
            session.setAttribute("cartList", carts);
        }
        return result;
    }
//    /**
//     * 根据购物车编号删除商品
//     * @param cno
//     * @return
//     */
    @RequestMapping(value = "delCart",method = RequestMethod.GET)
    public int delCart(@RequestParam String cno, HttpSession session){
        CartInfo cartInfo = new CartInfo();
        cartInfo.setCno(cno);
        int result = cartInfoBiz.del(cartInfo);
        if (result > 0) {
            //重新查询购物车中数据，更新session
            MemberInfo member = (MemberInfo) session.getAttribute("user");
            List<Map<String, Object>> carts = cartInfoBiz.findByMno(member.getMno());
            session.setAttribute("cartList", carts);
        }
        return result;
    }
}
